##rem $Id: C_c.sd,v 1.2 1999/11/28 03:36:41 carlos Exp $
##stringtype C
##shortstrings
/****************************************************************
   This file was created automatically by `%fv'
   from "%f0".

   Do NOT edit by hand!
****************************************************************/

/****************************************************************
    This file uses the auto initialization possibilities of
    Dice, gcc and SAS/C, respectively.

    Dice does this by using the keywords __autoinit and
    __autoexit, SAS uses names beginning with _STI or
    _STD, respectively. gcc uses the asm() instruction,
    to emulate C++ constructors and destructors.

    Using this file you don't have *all* possibilities of
    the locale.library. (No Locale or Language arguments are
    supported when opening the catalog. However, these are
    *very* rarely used, so this should be sufficient for most
    applications.
****************************************************************/


/*
    Include files and compiler specific stuff
*/
#include <exec/memory.h>
#include <libraries/locale.h>
#include <libraries/iffparse.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/locale.h>
#include <proto/utility.h>
#include <proto/iffparse.h>

#include <stdlib.h>
#include <string.h>



#include "%b_cat.h"


/*
    Variables
*/
struct FC_String %b_Strings[%n] = {
    { (STRPTR) %s, %d }%(,)
};

STATIC struct Catalog *%bCatalog = NULL;
#ifdef LOCALIZE_V20
STATIC STRPTR %bStrings = NULL;
STATIC ULONG %bStringsSize;
#endif


#if defined(_DCC)
STATIC __autoexit VOID _STDClose%bCatalog(VOID)
#elif defined(__SASC)
VOID _STDClose%bCatalog(VOID)
#elif defined(__GNUC__)
STATIC VOID _STDClose%bCatalog(VOID)
#else
VOID Close%bCatalog(VOID)
#endif

{
    if (%bCatalog) {
	CloseCatalog(%bCatalog);
    }
#ifdef LOCALIZE_V20
    if (%bStrings) {
	FreeMem(%bStrings, %bStringsSize);
    }
#endif
}


#if defined(_DCC)
STATIC __autoinit VOID _STIOpen%bCatalog(VOID)
#elif defined(__SASC)
VOID _STIOpen%bCatalog(VOID)
#elif defined(__GNUC__)
VOID _STIOpen%bCatalog(VOID)
#else
VOID Open%bCatalog(VOID)
#endif

{
    if (LocaleBase) {
	if ((%bCatalog = OpenCatalog(NULL, (STRPTR) "%b.catalog",
				     OC_BuiltInLanguage, %l,
				     OC_Version, %v,
				     TAG_DONE))) {
	    struct FC_String *fc;
	    int i;

	    for (i = 0, fc = %b_Strings;  i < %n;  i++, fc++) {
		 fc->msg = GetCatalogStr(%bCatalog, fc->id, (STRPTR) fc->msg);
	    }
	}
    }
}

#if defined(__GNUC__)
__asm ("  .text;  .stabs \\\"___CTOR_LIST__\\\",22,0,0,__STIOpen%bCatalog");
__asm ("  .text;  .stabs \\\"___DTOR_LIST__\\\",22,0,0,__STDClose%bCatalog");
#endif



#ifdef LOCALIZE_V20
VOID Init%bCatalog(STRPTR language)

{
    struct IFFHandle *iffHandle;

    /*
    **  Use iffparse.library only, if we need to.
    */
    if (LocaleBase  ||  !IFFParseBase  ||  !language  ||
	Stricmp(language, %l) == 0) {
	return;
    }

    if ((iffHandle = AllocIFF())) {
	char path[128]; /* Enough to hold 4 path items (dos.library 3.1)    */
	strcpy(path, "PROGDIR:Catalogs");
	AddPart((STRPTR) path, language, sizeof(path));
	AddPart((STRPTR) path, "%b.catalog", sizeof(path));
	if (!(iffHandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE))) {
	    strcpy(path, "LOCALE:Catalogs");
	    AddPart((STRPTR) path, language, sizeof(path));
	    AddPart((STRPTR) path, language, sizeof(path));
	    iffHandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE);
	}

	if (iffHandle->iff_Stream) {
	    InitIFFasDOS(iffHandle);
	    if (!OpenIFF(iffHandle, IFFF_READ)) {
		if (!PropChunk(iffHandle, MAKE_ID('C','T','L','G'),
			       MAKE_ID('S','T','R','S'))) {
		    struct StoredProperty *sp;
		    int error;

		    for (;;) {
			if ((error = ParseIFF(iffHandle, IFFPARSE_STEP))
				   ==  IFFERR_EOC) {
			    continue;
			}
			if (error) {
			    break;
			}

			if ((sp = FindProp(iffHandle, MAKE_ID('C','T','L','G'),
					   MAKE_ID('S','T','R','S')))) {
			    /*
			    **  Check catalog and calculate the needed
			    **  number of bytes.
			    **  A catalog string consists of
			    **      ID (LONG)
			    **      Size (LONG)
			    **      Bytes (long word padded)
			    */
			    LONG bytesRemaining;
			    LONG *ptr;

			    %bStringsSize = 0;
			    bytesRemaining = sp->sp_Size;
			    ptr = (LONG *) sp->sp_Data;

			    while (bytesRemaining > 0) {
				LONG skipSize, stringSize;

				ptr++;                  /*  Skip ID     */
				stringSize = *ptr++;
				skipSize = ((stringSize+3) >> 2);

				%bStringsSize += stringSize+1;  /*  NUL */
				bytesRemaining -= 8 + (skipSize << 2);
				ptr += skipSize;
			    }

			    if (!bytesRemaining  &&
				(%bStrings = AllocMem(%bStringsSize, MEMF_ANY))) {
				STRPTR sptr;

				bytesRemaining = sp->sp_Size;
				ptr = (LONG *) sp->sp_Data;
				sptr = %bStrings;

				while (bytesRemaining) {
				    LONG skipSize, stringSize, id;
				    struct FC_String *fc;
				    int i;

				    id = *ptr++;
				    stringSize = *ptr++;
				    skipSize = ((stringSize+3) >> 2);

				    CopyMem(ptr, sptr, stringSize);
				    bytesRemaining -= 8 + (skipSize << 2);
				    ptr += skipSize;

				    for (i = 0, fc = %b_Strings;  i < %n;  i++, fc++) {
					if (fc->id == id) {
					    fc->msg = sptr;
					}
				    }

				    sptr += stringSize;
				    *sptr++ = '\\0';
				}
			    }
			    break;
			}
		    }
		}
		CloseIFF(iffHandle);
	    }
	    Close(iffHandle->iff_Stream);
	}
	FreeIFF(iffHandle);
    }
}
#endif
